//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.二分查找;

public class demo_2529 {
    public demo_2529() {
    }

    public static void main(String[] args) {
        int[] nums = new int[]{-6, -5, -4, -3, 4, 5, 7, 8, 9, 10};
        System.out.println(maximumCount(nums));
    }

    public static int maximumCount(int[] nums) {
        int n = nums.length;
        if ((nums[0] >= 0 || nums[n - 1] >= 0) && (nums[0] <= 0 || nums[n - 1] <= 0)) {
            if (nums[0] == 0 && nums[n - 1] == 0) {
                return 0;
            } else {
                int plusL = 0;
                int plusR = n - 1;
                int minusL = 0;
                int minusR = n - 1;

                while(plusL < plusR || minusL < minusR) {
                    if (plusL < plusR) {
                        int plusMid = plusL + (plusR - plusL) / 2;
                        if (nums[plusMid] <= 0) {
                            plusL = plusMid + 1;
                        } else {
                            plusR = plusMid;
                        }
                    }

                    if (minusL < minusR) {
                        int minusMid = minusL + (minusR - minusL + 1) / 2;
                        if (nums[minusMid] >= 0) {
                            minusR = minusMid - 1;
                        } else {
                            minusL = minusMid;
                        }
                    }
                }

                int plus = n - plusL;
                int minus = minusR + 1;
                return Math.max(plus, minus);
            }
        } else {
            return n;
        }
    }
}
